﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace _07MindFile.Middleware
{
    public static class MindMapHandler
    {
        public static void HandleMindFileMap(IApplicationBuilder app)
        {
            app.Run(async context =>
            {
                var containsMindMap = context.Request.Path.Value.Contains(".mindmap",
                StringComparison.InvariantCultureIgnoreCase);
                if (containsMindMap)
                {
                    var path = context.Request.Path.Value.Substring(context.Request.Path.Value.LastIndexOf(@"/") + 1);
                    if (!context.Response.HasStarted)
                    {
                        context.Response.Redirect($"/MindFile?path={path}");
                    }
                }
            });
        }
    }
}
